package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.model.FactoryQueryCandidates;
import ru.rchervot.sanasar.client.model.UserSettings;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;
import ru.rchervot.sanasar.client.view.Presenter.Display;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Provider;

public class FactoryModuleCandidatesDefault implements FactoryModuleCandidates {

	private final HandlerManager eventBus;
	private final RPCServiceAsync service;
	private final Provider<RecordEditPresenter> providerRecordEdit;
	private final FactorySearchHistoryView factoryHistory;
	private final FactorySearchResultsView factoryResults;
	private final FactoryQueryCandidates factoryQuery;
	private final FactoryFiltersPanelViewCandidates factoryFiltersPanel;
	
	private UserSettings settings;
	
	@Inject
	public FactoryModuleCandidatesDefault(
			HandlerManager eventBus, RPCServiceAsync service,
			Provider<RecordEditPresenter> providerRecordEdit,
			FactorySearchHistoryView factoryHistory,
			FactorySearchResultsView factoryResults,
			FactoryQueryCandidates factoryQuery,
			FactoryFiltersPanelViewCandidates factoryFiltersPanel) {
		
		this.eventBus = eventBus;
		this.service = service;
		this.providerRecordEdit = providerRecordEdit;
		this.factoryHistory = factoryHistory;
		this.factoryResults = factoryResults;
		this.factoryQuery = factoryQuery;
		this.factoryFiltersPanel = factoryFiltersPanel;
	}
	
	private ModuleCandidatesView moduleView;
	private ModuleCandidatesView getModuleView(UserSettings settings) {
		if (moduleView == null) {
			moduleView = new ModuleCandidatesView(
					getModuleHeader(),
					getMessagesView(),
					getSectionsView());
			
			ModuleCandidatesPresenter p = new ModuleCandidatesPresenter(
					moduleView, eventBus, settings);
			p.bindDisplay();
		}
		
		return moduleView;
	}
	
	private ModuleHeaderSearchCandidatesView moduleHeaderView;
	private Widget getModuleHeader() {
		if (moduleHeaderView == null) {
			moduleHeaderView = new ModuleHeaderSearchCandidatesView(getSearchBox());
			ModuleHeaderSearchCandidatesPresenter p = 
				new ModuleHeaderSearchCandidatesPresenter(moduleHeaderView, 
						eventBus);
			p.bindDisplay();
		}
		
		return moduleHeaderView;
	}
	
	private SearchBoxCandidatesView searchBoxView;
	private Widget getSearchBox() {
		if (searchBoxView == null) {
			searchBoxView = new SearchBoxCandidatesView(factoryFiltersPanel.getFiltersPanelView());
			SearchBoxCandidatesPresenter p = new SearchBoxCandidatesPresenter(searchBoxView,
					eventBus, service, factoryQuery);
			p.bindDisplay();			
		}
		
		return searchBoxView;
	}
	
	private SectionsSearchCandidatesView sectionsView;
	private Widget getSectionsView() {
		if (sectionsView == null) {
			sectionsView = new SectionsSearchCandidatesView();
			SectionsSearchCandidatesPresenter p = 
				new SectionsSearchCandidatesPresenter(sectionsView, eventBus);
			p.bindDisplay();
		}
		
		return sectionsView;
	}
	
	private MessagesViewDefault messagesView;
	private Widget getMessagesView() {
		if (messagesView == null) {
			messagesView = new MessagesViewDefault();
			MessagesPresenter p = new MessagesPresenterDefault(
					messagesView, eventBus);
			p.bindDisplay();
		}

		return messagesView;
	}
	
	@Override
	public Display getViewForPlaceSearch() {
		getModuleView(settings).setContent(
				factoryResults.build(settings.getPageSize()));
		
		return getModuleView(settings);
	}

	@Override
	public Display getViewForPlaceHistory() {
		getModuleView(settings).setContent(
				factoryHistory.build());
		
		return getModuleView(settings);
	}

	@Override
	public void setSettings(UserSettings settings) {
		this.settings = settings;
	}

}
